//
// MachTimer.h
//

#include <assert.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>

@interface MachTimer : NSObject {
    uint64_t t0;
}

- (void)start;
- (uint64_t)elapsed;
- (float)elapsedSec;

@end

//
// MachTimer.m
//

#import "MachTimer.h"

static mach_timebase_info_data_t timebase;

@implementation MachTimer
+ (void)initialize
{
    (void) mach_timebase_info(&timebase);
}


- init
{
    if(self = [super init]) {
        t0 = mach_absolute_time();
    }
    return self;
}

- (void)start
{
    t0 = mach_absolute_time();
}

- (uint64_t)elapsed {
    return mach_absolute_time() - t0;
}

- (float)elapsedSec {
    return ((float)(mach_absolute_time() - t0)) * ((float)timebase.numer) / ((float)timebase.denom) / 1000000000.0f;
}
@end